home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- *
- * Generic Appshell- a system 7.0 compatible application shell
- *
- * ©1992 Graham Cox. All Rights Reserved.
- *
- * This shell can be used as a basis for any application in THINK C- just add
- * procedures as required. To minimise the need to alter this main file, the
- * code is split into two sections- the main loop section and the code dispatching
- * section. The code dispatch will need modification for each application, but the
- * main section shouldnt. This shell supports standard apple events, DA's and
- * all the usual malarkey. In addition, one resizeable non-specific window is
- * opened, and dialogs provided for the about box, and printing. Uses resources
- * from file "Generic Appshell.rsrc" which must be available to run this shell.
- *
- * Modification History:
- * 10/2/92 created from scratch.
- *
- *
- *
- *************************************************************************************/
- #include "GestaltEqu.h"
- #include "Traps.h"
- #include "PDPGlobalEqu.p"
- #include "AppleEvents.h"
-
- #include "PDP8_Main.proto.h"
-
- /* Global Variables. You should try to minimise the number of these by planning your
- code appropriately. */
-
- MenuHandle AppMenus[NumMenus]; /* Handles to the menus */
- int done = FALSE; /* Flag, App quits when true. */
- int AsmError = 0; /* assembly error code */
-
- pascal OSErr OpenApplication(AppleEvent *theAEvt,AppleEvent *reply,long hRefCon);
- pascal OSErr OpenDocuments(AppleEvent *theAEvt,AppleEvent *reply,long hRefCon);
- pascal OSErr PrintDocuments(AppleEvent *theAEvt,AppleEvent *reply,long hRefCon);
- pascal OSErr QuitApplication(AppleEvent *theAEvt,AppleEvent *reply,long hRefCon);
-
-
- /* fixed code follows- in theory, this should not require modification */
-
-
- pascal long ShowCriticalMem(Size cbNeeded)
- {
- /* critical memory alert message */
- int aHit;
-
- aHit = xAlert(CriticalMemoryAlert,NIL);
- return(NIL);
- }
-
-
- SysInitialize(void)
- {
- /* required system initialisation */
-
- int canUseGestalt,index;
- long gResponse;
- OSErr theErr;
-
- InitGraf(&thePort); /* mac toolbox init stuff */
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL);
-
- SetWatchCursor();
- FlushEvents(everyEvent,0);
-
- for (index = 0;index<8;index++)
- MoreMasters();
- SetGrowZone(&ShowCriticalMem);
-
- InitxAlert(TRUE); /* allows correct positioning of alerts */
-
- if (GestaltAvailable()) {
- theErr = Gestalt(gestaltAppleEventsAttr,&gResponse);
- if (theErr == noErr && (gResponse & 1)) {
- /* OK- Apple events are available, so install the standard ones */
-
- theErr = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
- &OpenApplication,0,FALSE);
- theErr = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
- &OpenDocuments,0,FALSE);
- theErr = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
- &PrintDocuments,0,FALSE);
- theErr = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
- &QuitApplication,0,FALSE);
- }
- else
- OpenNewWindow();
- }
- else
- OpenNewWindow();
-
- }
-
-
- main(void)
- {
- /* application entry point to main program */
-
- SysInitialize(); /* init system */
- AppInitialize(); /* init app specific stuff */
-
- GetAndProcessEvents(); /* run the program */
-
- CleanUp(); /* tail-end good housekeeping */
- ExitToShell(); /* return from this process */
- }
-
-
- int NumToolTraps(void)
- {
- /* returns number of toolbox traps */
-
- if (NGetTrapAddress(_InitGraf,ToolTrap) ==
- NGetTrapAddress(0xAA6E,ToolTrap))
- return(0x0200);
- else
- return(0x0400);
- }
-
- #define trapMask 0x0800
-
-
- int GetTrapType(int theTrap)
- {
- /* returns type of the trap */
-
- if (BitAnd(theTrap,trapMask) > 0)
- return(ToolTrap);
- else
- return(OSTrap);
- }
-
-
- short TrapAvailable(int theTrap)
- {
- /* general function for testing trap availability */
-
- int tType;
-
- tType = GetTrapType(theTrap);
- if (tType == ToolTrap) {
- theTrap &= 0x07FF;
- if (theTrap >= NumToolTraps())
- theTrap = _Unimplemented;
- }
- return(NGetTrapAddress(theTrap,tType) != NGetTrapAddress(_Unimplemented,ToolTrap));
- }
-
-
- short WNEAvailable(void)
- {
- /* returns true if WNE is available */
-
- return(TrapAvailable(_WaitNextEvent));
- }
-
-
- short GestaltAvailable(void)
- {
- /* returns true if Gestalt is available */
-
- return(TrapAvailable(_GestaltDispatch));
- }
-
-
- GetAndProcessEvents(void)
- {
- /* main event fetch loop. Uses WNE if possible, else GNE */
-
- EventRecord theEvent;
- OSErr theErr;
- short UseWNE;
- RgnHandle CursRgn;
-
- CursRgn = NewRgn();
- UseWNE = WNEAvailable();
-
- while (!done) {
-
- RunPDP(FrontWindow());
-
- if (UseWNE) {
- AdjustCursor(theEvent.where,CursRgn);
- if (WaitNextEvent(everyEvent,&theEvent,4,NIL))
- DoEvent(&theEvent);
- }
- else {
- SystemTask();
- AdjustCursor(theEvent.where,CursRgn);
- if (GetNextEvent(everyEvent,&theEvent))
- DoEvent(&theEvent);
- }
- }
- }
-
-
- DoEvent(EventRecord* theEvent)
- {
- /* dispatch raw events to process routines */
-
- switch (theEvent->what) {
- case mouseDown:
- ProcessClick(theEvent);
- break;
- case keyDown:
- ProcessKey(theEvent);
- break;
- case autoKey:
- ProcessAutoKey(theEvent);
- break;
- case activateEvt:
- ProcessActivate(theEvent);
- break;
- case updateEvt:
- ProcessUpdate(theEvent);
- break;
- case osEvt:
- ProcessOSEvt(theEvent);
- break;
- case kHighLevelEvent:
- ProcessHLE(theEvent);
- break;
- case diskEvt:
- {
- long emCode;
- int dmResult;
- Point where;
-
- emCode = theEvent->message;
- if (HiWord(emCode) != noErr) {
- SetPt(&where,100,100);
- dmResult = DIBadMount(where,emCode);
- }
- }
- break;
- default:
- break;
- }
- }
-
-
- ProcessClick(EventRecord *theEvent){
- /* dispatch mouse clicks */
-
- int mClick,partCode;
- WindowPtr theWindow;
- Point mPoint;
- long mSelect;
-
- mPoint = theEvent->where;
- mClick = FindWindow(mPoint,&theWindow);
- switch (mClick) {
- case inDesk:
- break;
- case inMenuBar:
- MenuAdjust();
- mSelect = MenuSelect(mPoint);
- if (HiWord(mSelect) != 0)
- DispatchMenu(mSelect);
- HiliteMenu(0);
- break;
- case inSysWindow:
- SystemClick(theEvent,theWindow);
- break;
- case inContent:
- ClickTheWindow(theWindow,mPoint);
- break;
- case inDrag:
- DragTheWindow(theWindow,mPoint);
- break;
- case inGrow:
- GrowTheWindow(theWindow,mPoint);
- break;
- case inGoAway:
- if (TrackGoAway(theWindow,mPoint))
- CloseTheWindow(theWindow);
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(theWindow,mPoint,mClick))
- ZoomTheWindow(theWindow,mClick);
- break;
- default:
- break;
- }
- }
-
-
- ProcessKey(EventRecord *theEvent){
- /* gets character typed. If menu command, calls user menu selection function */
- char theKey;
- long mSelect;
-
- theKey = LoWord(theEvent->message) & charCodeMask;
- if ((theEvent->modifiers) & cmdKey) {
- mSelect = MenuKey(theKey);
- if (HiWord(mSelect) != 0)
- DispatchMenu(mSelect);
- HiliteMenu(0);
- }
- /* add code to use the key event as required */
- }
-
-
- ProcessAutoKey(EventRecord *theEvent){
- char theKey;
-
- theKey = LoWord(theEvent->message) & charCodeMask;
- /* add code to handle the autokey event as required */
- }
-
-
- ProcessActivate(EventRecord *theEvent){
- /* resolves activation type and calls appropriate user functions */
-
- WindowPtr theWindow;
- int actState;
-
- theWindow = (WindowPtr)theEvent->message;
- actState = theEvent->modifiers & activeFlag;
- if (actState)
- ActivateTheWindow(theWindow);
- else
- DeactivateTheWindow(theWindow);
- }
-
-
- ProcessUpdate(EventRecord *theEvent)
- {
- /* resolves target for update, sets the port to this, calls user function */
-
- WindowPtr theWindow;
- GrafPtr savePort;
-
- theWindow = (WindowPtr)theEvent->message;
- if (theWindow != NIL) {
-
- GetPort(&savePort);
- SetPort(theWindow);
- BeginUpdate(theWindow);
- DrawTheWindow(theWindow);
- EndUpdate(theWindow);
- SetPort(savePort);
- }
- }
-
-
- ProcessOSEvt(EventRecord *theEvent){
- /* does suspend/resume event if enabled for this application (it should be) */
-
- int srState;
-
- if (HiWord(theEvent->message) & 0x0100) {
- srState = theEvent->message & 1;
-
- if (srState)
- ResumeApplication();
- else
- SuspendApplication();
- }
- }
-
-
- ProcessHLE(EventRecord *theEvent){
- /* processes Apple Events registered at Init time- for generic shell, only the
- required four kinds are set up, and most are dummy code shells */
-
- OSErr theErr;
-
- theErr = AEProcessAppleEvent(theEvent);
- if (theErr != noErr)
- SysBeep(1);
- }
-
-
- AdjustCursor(Point where,RgnHandle cursRgn)
- {
- InitCursor();
- }
-
-
- CleanUp(void)
- {
-
-
- }